const http = require("http");
const fs = require("fs");
const path = require("path");
const mysql = require("mysql");

http.createServer((req, res) => {
    var jurl = req.url;
    console.log(jurl);

    if (jurl == "/") {
        var filePath = path.join(__dirname, "/chapterList.html");
        res.writeHead(200, { "Content-Type": "text/html;charset=utf-8" });
        var fileContent = fs.readFileSync(filePath);
        res.write(fileContent);
        res.end();
    } else if (jurl == "/getChapterList") {
        // 从数据库里面获取数据
        var con = mysql.createConnection({
            host: "192.168.182.144",
            port: 3306,
            user: "root",
            password: "ddd",
            database: "mydatabase",
        });

        var selectSql = "select * from chapters";
        con.query(selectSql, (err, result) => {
            if (err) {
                console.log(err);
            } else {
                console.log(result);
                var jres = JSON.stringify(result);
                res.writeHead(200, { "Content-Type": "text/plain;charset=utf-8" });
                res.write(jres);
                res.end();
            }
        });
    } else if (jurl.indexOf("css/") >= 0) {
        // 将css文件写入
        var filePath = path.join(__dirname, "/" + jurl);
        var fileContent = fs.readFileSync(filePath);
        res.writeHead(200, { "Content-Type": "text/css;charset=utf-8" });
        res.write(fileContent);
        res.end();
    } else if (jurl.indexOf("/baiduTemplate.js") >= 0) {
        // 将js文件写入
        var filePath = path.join(__dirname, "/js/baiduTemplate.js");
        var fileContent = fs.readFileSync(filePath);
        res.writeHead(200, { "Content-Type": "text/js;charset=utf-8" });
        res.write(fileContent);
        res.end();
    } else if (jurl.indexOf("images/") >= 0) {
        // 将图片文件写入
        var filePath = path.join(__dirname, "/" + jurl);
        var fileContent = fs.readFileSync(filePath);
        res.writeHead(200, { "Content-Type": "image/*;charset=utf-8" });
        res.write(fileContent);
        res.end();
    } else if (jurl.indexOf("/getDetail") >= 0) {
        // 特定数据请求
        var con = mysql.createConnection({
            host: "192.168.182.144",
            port: 3306,
            user: "root",
            password: "ddd",
            database: "mydatabase",
        });
        var selectSql = "select * from chapters from where chapterid=?";
        var id = jurl.split("=")[1];
        con.query(selectSql, [id], (err, result) => {
            if (err) {
                console.log(err);
            } else {
                var jres = JSON.stringify(result);
                res.setHeader("Access-Control-Allow-Origin", "*");
                res.writeHead(200, { "Content-Type": "text/plain;charset=utf-8" });
                res.write(jres);
                res.end();
            }
        });
    }
}).listen(8084);

console.log("server listening on 8084");
